package com.lepu.app.fun.my.bean;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;

public class BeanSearchHistory {

private static final String TABLE_NAME = "SearchHistory";
	
	public int Id;
	
	public String Uid;
	public String Name;
	public int Type;//no use
	public long Time;
	
	public static boolean insertItem(BeanSearchHistory item) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		ContentValues values = new ContentValues();
		values.put("Uid", item.Uid);
		values.put("Name", item.Name);
		values.put("Type", item.Type);
		values.put("Time", item.Time);

		flag = dbHelp.insert(TABLE_NAME, null, values);

		return flag;
	}
	
	public static boolean checkItem(String uid,String name) {
		boolean flag = false;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		String sql = "select * from SearchHistory where Uid=? and Name=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] {uid,String.valueOf(name) });

		if (queryResult != null && queryResult.size() > 0) {
			flag = true;
		}

		return flag;
	}
	
	public static boolean deleteItem(String uid,String name) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=? and Name=?",
				new String[] { uid,String.valueOf(name) });

		return flag;
	}
	
	public static boolean deleteAllItem(String uid) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		flag = dbHelp.delete(TABLE_NAME, "Uid=?",
				new String[] { uid });

		return flag;
	}
	
	/*
	 * 当前用户是否有自己的图片相册
	 */
	public static ArrayList<BeanSearchHistory> getAllList(String uid) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return null;

		String sql = "select * from SearchHistory where Uid=? order by Id desc";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] {uid});

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<BeanSearchHistory> dataList = new ArrayList<BeanSearchHistory>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			BeanSearchHistory item = BeanSearchHistory.parseFromDatabase(dictionary);
			dataList.add(item);
		}
		return dataList;
	}

	private static BeanSearchHistory parseFromDatabase(
			HashMap<String, Object> dictionary) {
		
		if (dictionary == null || dictionary.size() == 0) {
			return null;
		}
		BeanSearchHistory item = new BeanSearchHistory();
		
		item.Id = UtilityBase.parseInt((String) dictionary.get("Id"));
		item.Uid = UtilityBase.parseString((String) dictionary.get("Uid"));
		item.Name = UtilityBase.parseString((String) dictionary.get("Name"));
		item.Type = UtilityBase.parseInt((String) dictionary.get("Type"));
		item.Time = UtilityBase.parseLong((String) dictionary.get("Time"));
		
		return item;
	}
}
